using Light;
using Light.Assets.Scripts.EventManager;
using UnityEngine;

public class Death : CoreComponent
{
    [SerializeField] private GameObject[] deathParticles;
    [SerializeField] private Animator animator;

    [SerializeField] private AudioClip deathAudio;

    private ParticleManager particleManager;

    private ParticleManager ParticleManager =>
        particleManager ? particleManager : core.GetCoreComponent<ParticleManager>();

    private void OnEnable()
    {
        EventManager.Register<DeathEvent>(Die);
    }

    public void Die(DeathEvent evt)
    {
        if (evt.transform == core.transform)
        {
            foreach (var particle in deathParticles) ParticleManager.StartParticles(particle);


            using var evt2 = EnemyDeleteEvent.Get();
            evt2.transform = core.transform.parent.gameObject.transform;
            EventManager.SendEvent(evt2);

            AudioManager.Instance.PlaySFX(deathAudio);

            animator.SetTrigger("Death");

            //core.transform.parent.gameObject.SetActive(false);
        }
    }
}